package dev.pda.display.presentation.page.widgets

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.widget.ImageView
import android.widget.LinearLayout
import com.bumptech.glide.Glide
import dev.pda.display.presentation.page.model.widget.ImageWidget

class CustomImageView : LinearLayout {

    private var imageView: ImageView? = null

    companion object {
        private const val HTTP = "http"
        private const val HTTPS = "https://"
    }

    constructor(context: Context?) : super(context) {
        init(null)
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init(attrs)
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun init(attrs: AttributeSet?) {
        imageView = ImageView(context)
        this.addView(imageView)
    }

    fun setImageViewMethod(widget: ImageWidget) {
        imageView?.apply {
            scaleType = ImageView.ScaleType.FIT_XY
        }
        val imageUrl = "http" + widget.item?.imgUrl
        imageView?.let { Glide.with(context).load(imageUrl).into(it) }
    }
}